"""
@author: 江同学呀
@file: alias_field.py
@date: 2024/7/26 11:19
@desc:
    https://www.elastic.co/guide/en/elasticsearch/reference/7.17/field-alias.html

    Defines an alias for an existing field.
    定义现有字段的别名。
"""
from abc import ABC
from typing import Type, Union, Dict, Any

from espc.orm.model.base.base import _Base
from espc.orm.model.mapping.field.base_field.base_field import _BaseField
from espc.orm.model.mapping.field.field_attribute import PathAttribute
from espc.utils.build_add_attr import build_add_attr


class AliasField(_BaseField, PathAttribute, ABC):
    """
    别名 字段
    todo 特殊字段类型，未仔细研究文档
    """
    _field_type: str = "alias"

    def __init__(
            self, field_name: str = None, value: Any = None, default: Any = None, path: _BaseField = None
    ):
        super().__init__(field_name=field_name, value=value, default=default, path=path)
        return

    def __get__(self, instance: _Base, owner: Type["AliasField"]) -> Union["AliasField", Any]:
        return super(AliasField, self).__get__(instance=instance, owner=owner)

    def __set__(self, instance, value: Any):
        super(AliasField, self).__set__(instance=instance, value=value)
        return

    @property
    def value(self) -> Any:
        return self._value

    @value.setter
    def value(self, value: Any):
        self._value: Any = value
        return

    @build_add_attr
    def _build(self) -> Dict:
        mapping: Dict = {
            "type": self._field_type,
        }
        return mapping
